package com.masterclient.app.modules.firstpager.fragments.adapters;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.masterclient.app.App;
import com.masterclient.app.R;
import com.eoner.baselibrary.bean.firstpager.NewProductDiscountBean;
import com.eoner.baselibrary.utils.ScreenUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：sean
 * 创建时间：2017/8/15 16:20
 * 功能说明：
 */

public class NewGoodsGridAdapter extends BaseAdapter{

    private Context context;
    List<NewProductDiscountBean.DataBean.ShProductsBean> list;
    public NewGoodsGridAdapter(Context context) {
        this.context = context;
    }

    public void setList(List<NewProductDiscountBean.DataBean.ShProductsBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        NewProductDiscountBean.DataBean.ShProductsBean item = list.get(position);

        View view = View.inflate(context,R.layout.item_new_good_discount,null);
        ViewHolder holder = new ViewHolder(view);
        App.picasso.load(item.getSh_image()).fit().into(holder.ivGoods);
        holder.tvTitle.setText(item.getSh_name());
        holder.tvSubTitle.setText(item.getSh_subname());
        holder.tvPriceNow.setText("¥"+item.getSh_show_price());
        holder.tvPriceOlder.setText("¥"+item.getSh_line_price());
        holder.tvPriceOlder.setPaintFlags(Paint. STRIKE_THRU_TEXT_FLAG| Paint.ANTI_ALIAS_FLAG);

        if(item.getSh_label()!=null&& !TextUtils.isEmpty(item.getSh_label().getSh_icon_height())&& !TextUtils.isEmpty(item.getSh_label().getSh_icon())){
            App.picasso.load(item.getSh_label().getSh_icon()).fit().into(holder.ivActivity);
            holder.ivActivity.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.ivActivity.getLayoutParams();
            params.height = ScreenUtils.dp2px(Integer.valueOf(item.getSh_label().getSh_icon_height())/2);
            params.width = ScreenUtils.dp2px(Integer.valueOf(item.getSh_label().getSh_icon_width())/2);
            switch (item.getSh_label().getSh_location()){
                case "left_top":
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    break;
                case "right_top":
                    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    break;
                case "left_bottom":
                    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    params.setMargins(0,0,ScreenUtils.dp2px(7),ScreenUtils.dp2px(7));
                    break;
                case "right_bottom":
                    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                    params.setMargins(0,0,ScreenUtils.dp2px(7),ScreenUtils.dp2px(7));
                    break;
            }
            holder.ivActivity.setLayoutParams(params);
        }
        return view;
    }


    static class ViewHolder {
        @BindView(R.id.iv_goods)
        ImageView ivGoods;
        @BindView(R.id.tv_sub_title)
        TextView tvSubTitle;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_price_now)
        TextView tvPriceNow;
        @BindView(R.id.tv_price_older)
        TextView tvPriceOlder;
        @BindView(R.id.iv_activity)
        ImageView ivActivity;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
